package com.taotao.service.Impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.taotao.common.resp.EasyUiResult;
import com.taotao.common.resp.TaotaoResult;
import com.taotao.common.util.IDUtils;
import com.taotao.dao.ItemDescMapper;
import com.taotao.dao.ItemMapper;
import com.taotao.dao.UserMapper;
import com.taotao.pojo.Item;
import com.taotao.pojo.ItemDesc;
import com.taotao.pojo.User;
import com.taotao.service.ItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * Created by zzb on 2017/11/3.
 */
//@Service(timeout = 3000)
//@org.springframework.stereotype.Service
public class ItemServiceImpl implements ItemService {


    @Autowired
    private ItemMapper itemMapper;

    @Autowired
    private ItemDescMapper itemDescMapper;



    public EasyUiResult findAllByPage(Integer page, Integer rows) {

        Integer count = itemMapper.totalCount();
        Integer startRow = (page - 1) * rows;
        List<Item> itemList = itemMapper.findByPage(startRow, rows);

        EasyUiResult easyUiResult = new EasyUiResult();
        easyUiResult.setTotal(count);
        easyUiResult.setRows(itemList);
        return easyUiResult;
    }

    @Transactional
    public TaotaoResult save(Item item, String desc) {

        Long itemId = IDUtils.genItemId();
        Date date = new Date();
        item.setId(itemId);
        //商品状态，1-正常，2-下架，3-删除
        item.setStatus((byte) 1);
        item.setCreated(date);
        item.setUpdated(date);

        itemMapper.insertSelective(item);

        ItemDesc itemDesc = new ItemDesc();
        itemDesc.setItemId(itemId);
        itemDesc.setItemDesc(desc);
        itemDesc.setCreated(date);
        itemDesc.setUpdated(date);

        itemDescMapper.insert(itemDesc);
        return TaotaoResult.ok();
    }

}
